import mysql.connector
from mysql.connector import Error
import urllib.parse

# 转义密码中的特殊字符（如 %）
password = '**********'
encoded_password = urllib.parse.quote_plus(password)

config = {
    'host': "1.1.1.1",
    'user': 'root',
    'password': encoded_password,
    'database': 'student_db',
    'port': 3306,
    'ssl_disabled': True  # 关闭 SSL，避免连接失败
}

try:
    conn = mysql.connector.connect(**config)
    if conn.is_connected():
        print("✅ 成功连接到数据库！")
        print("MySQL 版本:", conn.get_server_info())
except Error as e:
    print(f"❌ 数据库连接失败: {e}")
finally:
    if 'conn' in locals() and conn.is_connected():
        conn.close()
        print("🔗 数据库连接已关闭")